package com.glsc.ngateway.flowable.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableEhrEmp;
import com.glsc.ngateway.common.base.repo.mysql.flowable.FlowableEhrEmpRepo;
import com.glsc.ngateway.flowable.service.feignservice.PlatformFeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Deprecated
@Component
public class FlowableEhrEmpService {

    Logger logger = LoggerFactory.getLogger(FlowableEhrEmpService.class);

    @Autowired
    FlowableEhrEmpRepo repo;

    @Autowired
    FlowableEhrEmpService self;

    @Autowired
    PlatformFeignService platformFeignService;

    public List<FlowableEhrEmp> getUserList(String department) {
        if (StringUtils.isEmpty(department)) {
            return repo.findAll();
        } else {
            return repo.findByTitle(department);
        }
    }

    @Cacheable(cacheNames = "userInfoCache", key = "#userId", condition = "#userId !=null", unless = "#result==null")
    public FlowableEhrEmp getUserInfo(String userId) {
        FlowableEhrEmp emp = repo.findByBadge(userId);
        return emp;
    }

    public List<FlowableEhrEmp> getUserListByName(String name) {
        List<FlowableEhrEmp> list = repo.findByName(name);
        return list;
    }

    public List<FlowableEhrEmp> findAllByBadgeIn(List<String> badge) {
        return repo.findAllByBadgeIn(badge);
    }

    /**
     * cacheNames 设定要使用的cache的名字，必须提前定义好缓存
     * key 使用SpEL表达式来设定缓存的key，如果不设置默认方法上所有参数都会作为key的一部分
     * condition 使用SpEL表达式设定出发缓存的条件，在方法执行前生效
     * unless 使用SpEL设置不缓存的条件，这里是方法执行完生效，所以条件中可以有方法执行后的value   unless = "#result==#userId"
     * sync 用于同步的，在缓存失效（过期不存在等各种原因）的时候，如果多个线程同时访问被标注的方法则只允许一个线程通过去执行方法
     */
    @Cacheable(cacheNames = "userNameCache", key = "#userId", condition = "#userId != null")
    public String getChineseName(String userId) {
        logger.info("查询数据库获取用户名称:" + userId);
        if(StrUtil.isEmpty(userId)){
            return "";
        }
        String name = "";
        FlowableEhrEmp emp = repo.findByBadge(userId);
        if (emp != null) {
            name = emp.getName();
        } else {
            String platformName = platformFeignService.findByUserName(userId);
            if (StrUtil.isNotBlank(platformName)) {
                name = platformName;
            } else {
                name = userId;
            }
        }
        return name;
    }

    //separator 分隔符 , |
    public String getChineseNameAll(String userIdList, String separator) {
        if (StrUtil.isBlank(userIdList)) {
            return "";
        }
        return self.getChineseNameAll(StrUtil.splitTrim(userIdList, separator));
    }


    public String getChineseNameAll(List<String> userIdList) {
        List<String> names = userIdList.stream().map(self::getChineseName).collect(Collectors.toList());
        return CollectionUtil.join(names, "|");
    }
}
